#include "includes.h"
#include "pocs.h"/*
#include "ScanData.cpp"
#include "P1hilbert.cpp"
#include "P2compact.cpp"
#include "P3apriori.cpp"
#include "P4fproject.cpp"
#include "P5nonneg.cpp"
#include "testP4.cpp"*/

using namespace std;

int main (int argc, char* argv[]) 
{
	time_t t0, t1,t2;
	int i,j, numit=500;
	bool initalguess = true;
	double x=0,y=0,a=97,b=130;
	double dif;
	//double f = 54.889599/2.*348./888.;	
	double f = 54.889599/2.*250./888.;
	int fov = 128+64;
	// ng , gmin , gmax , nt, tmi, tma , rad, nx, ny
	//ScanData s = ScanData(348,-f/180.*M_PI , f/180.*M_PI, 984, 3.*M_PI_2, 3.*M_PI_2, 541, 512, 512,fov);
	ScanData s = ScanData(250,-f/180.*M_PI , f/180.*M_PI, 984, 3.*M_PI_2, 3.*M_PI_2, 541, 512, 512,fov);
	cout << "Reading data" <<endl;

	// Compact support data test
	for (i=0; i<s.Nx; i++){
		x = s.xmin + i*s.dx;
		for (j=0;j<s.Ny; j++){
			y = s.ymin + j*s.dy;
			if (x*x/a/a + y*y/b/b < 1.) s.CSmask[i][j] = true;
		}
	}
	j=0;i=0;
	ifstream file ("../../data/testTrunc.csv");
		
	file.precision(20);
		
	string line;
	while (getline (file, line))
	{
		istringstream linestream(line);
		string item;
		while (getline (linestream, item, ','))
		{
			s.data[j][i]  = (double) atof( item.c_str() );
			j++;
		}
		j=0;
		i++;
	}
	file.close();


	i = 0; j=0;
	file.open("../../data/APmask.csv",ifstream::in);
	while (getline (file, line))
	{
		istringstream linestream(line);
		string item;
		while (getline (linestream, item, ','))
		{
			s.APmask[i][j]  = (double) atoi( item.c_str() );
			j++;
		}
		j=0;
		i++;
	}
	file.close();

	i = 0; j=0;
	file.open("../../data/APdata.csv",ifstream::in);
	file.precision(20);
	while (getline (file, line))
	{
		istringstream linestream(line);
		string item;
		while (getline (linestream, item, ','))
		{
			s.APdata[i][j]  = (double) atof( item.c_str() );
			j++;
		}
		j=0;
		i++;
	}
	file.close();

	if(initalguess){
		i = 0; j=0;
		file.open("testPOCS.csv",ifstream::in);
		file.precision(20);
		while (getline (file, line))
		{
			istringstream linestream(line);
			string item;
			while (getline (linestream, item, ','))
			{
				s.recon[i][j]  = (double) atof( item.c_str() );
				j++;
			}
			j=0;
			i++;
		}
		file.close();
	}
	time(&t0);
	cout << "Preparing data: ";
	prepare(s);

	time(&t1);
	cout << " "<< difftime(t1,t0)<<" sec"<<endl;
	time(&t0);
	//P3apriori(s);
	//P1hilbert(s);
	for(i=1;i<=numit;i++){
		time(&t1);
		cout << "Iteration " << i << ": ";
		P1hilbert(s);
		P2compact(s);
		P3apriori(s);
		P4fproject(s);
		P5nonneg(s);
		time(&t2);
		cout << " "<< difftime(t2,t1)<<" sec"<<endl;
	}
	dif=difftime(t2,t0);
	cout << "Total running time: " << dif <<" sec"<<endl;
	cout << "Average running time: " << dif/numit <<" sec/iteration"<<endl; 
	cout << "Writing to file" << endl;
	// write to file
	ofstream outfile ("testPOCS.csv",ios::trunc);
	outfile.precision(20);
	for(i=0;i<s.Nx;++i){

		for(j=0;j<s.Ny;++j){
			outfile << s.recon[i][j] << ",";
			//outfile << s.FPdata[j] << ",";
		}
		outfile << "\n";
	}
	outfile.close();

	
	return 0;
}
